﻿using Mate.Caching;
using Mate.Db.Model.Good;
using Mate.Db.Model.Merchant;
using Mate.Db.SqlSugar;
using Mate.Dto.Good;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mate.Good.Service
{
    public class GoodService : DataService<mp_good>, IGoodService
    {
        private readonly ICacheManager _cache;
        public GoodService(DataClient dataClient, ICacheManager cache) : base(dataClient)
        {
            this._cache = cache;
        }

        #region 获取商品列表
        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="key">搜索关键字</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public IList<GoodModel> GetGoodList(int pageIndex, int pageSize, string key, ref int totalCount)
        {
            return dataClient.Queryable<mp_good, mp_good_category, mp_merchant>((g, gc, m) => new object[]
             {
                JoinType.Left,g.categoryid==gc.id,
                JoinType.Left,g.merchantid==m.id
             })
            .Where((g, gc, m) => g.is_delete == 1)
            .WhereIF(!string.IsNullOrWhiteSpace(key), (g, gc, m) => g.name.Contains(key) || g.title.Contains(key)||g.content.Contains(key))
            .OrderBy((g, gc, m) => g.id, OrderByType.Desc)
            .Select<GoodModel>("g.*,gc.name good_category_name,m.merchantname")
            .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        #endregion

        #region 获取商品详情
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="id">商品id</param>
        /// <returns></returns>
        public GoodModel GetGood(int id)
        {
            return dataClient.Queryable<mp_good, mp_good_category, mp_merchant>((g, gc, m) => new object[]
            {
                JoinType.Left,g.categoryid==gc.id,
                JoinType.Left,g.merchantid==m.id
            })
           .Where((g, gc, m) => g.is_delete == 1)
           .Select<GoodModel>("g.*,gc.name good_category_name,m.merchantname")
           .First();
        }
        #endregion
    }
}
